import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    currentStudent:{
      name: '',
      studentId: 0
    },
    time: 0,

    //TODO 数据结构改成数组内部3个数组
    dialogues: [[],[],[]],
    therapies: [[],[],[]],
    diagnosis: {
      //agreement和disagreement两个数组内部的元素都是String，以问题的key作为判断结果
      basic:[
        // {
        //   name:'亚急性感染性心内膜炎',
        //   agreement:[],
        // },
        // {
        //   name:'亚急性感染性心内膜炎加强版',
        //   agreement:[],
        // }
      ],
      checked:[
        // {
        //   name:'',
        //   agreement:[],
        //   disagreement:[]
        // }
      ]
    },
  },
  mutations: {
    login(state,params){
      state.currentStudent.name = params.name
      state.currentStudent.studentId = params.studentNo
    },
    nextScene (state) {
      state.time++
    },
    dialoguePush(state,params){
      // for(item in state.dialogues[state.time]){
      //   alert(item.q)
      //   if(params.q === item.q){
      //
      //     alert('已经提问或检查过，请在历史记录中查看')
      //     return
      //   }
      // }
      // state.dialogues[state.time].push(params)
      let bool = false
      state.dialogues[state.time].forEach((data=>{
        if (data.q === params.q){
          bool = true
        }
      }))
      if(!bool){
        state.dialogues[state.time].push(params)
      }else{
        alert('已经提问或检查过，请在历史记录中查看')
      }

    },
    diagnosisPush(state,params){
      let bool = false
      let index = 0
      state.diagnosis[params.type].forEach(((data,indexx)=>{
        if (data.name === params.name){
          bool = true
          index = indexx
        }
      }))
      if(!bool){
        //VUEX里面没有
        const res = {
          name: params.name,
          agreement: params.dataAgreement,
          disagreement: params.dataDisagreement
        }
        state.diagnosis[params.type].push(res)
      }else{
        //VUEX里面有（修改）
        state.diagnosis[params.type][index].agreement = params.dataAgreement
      }



    },
    diagnosisChange(state,params){

    },
    theraphyPush(state,params){
      state.therapies[state.time].push(params)
    },
    theraphyPop(state,params){
      state.therapies[state.time].remove(params)
    }
  },
  getters:{
    //这个getter获得所有的问题，用于添加诊断时候用
    fullyQuestions: function (state) {
      let result = []
      result = result.concat(state.dialogues[0])
      // alert(result)
      return result
    }
  }
})

export default store
